/**
 * Created by Administrator on 2017/6/8 0008.
 */

var utils = require("../common/utils");
var fs = require("fs");


exports.doLogin = function(parame,req,cb){
    var form = parame;
    console.log("doLogin from is;",form);

    var obj = {};
    var account = form.login_name.toLowerCase();
    var password = form.userpass;

    obj.commands = " SELECT * from tuser WHERE account = '" + account + "' and password = '" + password + "'";
;
    utils.connectToMysql(obj, function (info, err) {
        console.log("info is:",info)
        console.log("err is:",err)
        if (err) {
            jsonStr = {return_code:500, return_message:'用户名或密码不正确!'};
            return cb(jsonStr);
        }
        else {
            if(info.length <= 0 ){
                jsonStr = {return_code:500, return_message:'用户名或密码不正确!'};
                return cb(jsonStr);
            }
            var userInfo = info[0];
            req.session.user = {};
            req.session.user.username = userInfo.account;
           // req.session.user.isAdmin = userInfo.role;
            req.session.user.password = password;
           // req.session.user.power = JSON.parse(userInfo.power);

          //  if(req.session.user.power.appid.length < 0){
          //      var errInfo =  {return_code:500,return_message:"没有产品权限请联系管理员"}
          //      return cb(jsonStr);
          //  };

            var jsonStr = {return_code:200, return_message:'登入成功!',isAdmin:req.session.user.isAdmin };

            if(req.session.user.isAdmin == 1 ){
                req.session.user.app_id = 0
            }
            var loginip = req.headers['x-forwarded-for'] ||
                req.connection.remoteAddress ||
                req.socket.remoteAddress ||
                req.connection.socket.remoteAddress;

            var loginip = loginip.split(":");

            loginip = loginip[loginip.length - 1];
            fs.appendFileSync('../logs/loginLog/loginInfo.log', "\r\n" + JSON.stringify({account:form.account,ip:'127.0.0.1'}) + ",",null,2);
            return cb(jsonStr);
        }
    });
};


exports.check = function(parame,req,cb) {
    console.log("parame", parame);
    cb({return_message: '判断登入', return_code: 200});
}

